//
//  PublishButtonViewCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/29.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

protocol PublishButtonViewCellDelegate: NSObjectProtocol {
    func buttonViewCell(_ cell: PublishButtonViewCell, didClickedButton vm: PublishViewModel)
}

class PublishButtonViewCell: UITableViewCell {
    
    weak var delegate: PublishButtonViewCellDelegate?
    
    private var viewModel = PublishViewModel()
    
    private var _allergyHistory = ""

    @IBOutlet private weak var yesBtn: UIButton! {
        didSet {
            yesBtn.setTitleColor(.mainColor, for: .normal)
            yesBtn.setTitleColor(.white, for: .selected)
            yesBtn.setBackgroundImage(UIImage(color: .mainColorLight), for: .normal)
            yesBtn.setBackgroundImage(UIImage(color: .mainColor), for: .selected)
            yesBtn.layer.cornerRadius = yesBtn.height / 2
            yesBtn.layer.masksToBounds = true
        }
    }
    
    @IBOutlet private weak var noBtn: UIButton! {
        didSet {
            noBtn.setTitleColor(.mainColor, for: .normal)
            noBtn.setTitleColor(.white, for: .selected)
            noBtn.setBackgroundImage(UIImage(color: .mainColorLight), for: .normal)
            noBtn.setBackgroundImage(UIImage(color: .mainColor), for: .selected)
            noBtn.layer.cornerRadius = noBtn.height / 2
            noBtn.layer.masksToBounds = true
        }
    }
    
    @IBAction private func btnAction(_ sender: UIButton) {
        if sender.isSelected { return }
        
        if yesBtn == sender {
            noBtn.isSelected = false
            viewModel.isAllergyHistory = true
            viewModel.allergyHistory = _allergyHistory
        } else {
            yesBtn.isSelected = false
            viewModel.isAllergyHistory = false
            viewModel.allergyHistory = ""
        }
        
        delegate?.buttonViewCell(self, didClickedButton: viewModel)
    }
    
    func configure(with vm: PublishViewModel) {
        viewModel = vm
        if vm.isAllergyHistory {
            _allergyHistory = vm.allergyHistory
        }
        yesBtn.isSelected = vm.isAllergyHistory
        noBtn.isSelected = !vm.isAllergyHistory
    }
    
    func update(advisoryOrder vm: AdvisoryDetailViewModel) {
        yesBtn.isUserInteractionEnabled = false
        noBtn.isUserInteractionEnabled = false
        yesBtn.isSelected = vm.isAllergyHistory
        noBtn.isSelected = !vm.isAllergyHistory
    }
    
    func update(cloudOrder vm: MyCloudReadMeDetailViewModel) {
        yesBtn.isUserInteractionEnabled = false
        noBtn.isUserInteractionEnabled = false
        yesBtn.isSelected = vm.isAllergyHistory
        noBtn.isSelected = !vm.isAllergyHistory
    }
}
